import type { Module, ActionContext } from 'vuex'
import type { AxiosResponse } from 'axios'
import type { eventStateType } from './typings'
import {
  manholeAlarmListGet,
  manholeAlarmIgnoreAlarmGet,
  manholeAlarmPushOrderGet,
  manholeAlarmConfigGet,
  manholeAlarmConfigUpdateGet,
  manholeListGet,
  manholeExport,
  manholeImportExcelGet,
  manholeRemovePost,
  manholeViewGet,
  manholeUpdatePost,
  manholeSavePost,
  manholeSyncAssetManholeGet,
  manholeMapGetManholeTreeListGet,
  manholeMapGetManholePointAllGet,
  manholeMapGetDeviceStateNumGet,
  manholeMapGetDeviceListGet,
  manholeMapGetDeviceAlarmGet,
  manholeMapGetDeviceInfoGet,
  manholeWorkListGet
} from '~/api/monitor/manhole'
import { getTenantDictDataDictionaryByCode } from '~/api/global'
import { DownloadAttachment } from '~/utils/util'

const manholes: Module<eventStateType, eventStateType> = {
  namespaced: true,
  state: {
    manholeTypeOpt: []
  },
  getters: {
    manholeTypeOpt: (state) => state.manholeTypeOpt
  },
  mutations: {
    MANHOLE_TYPE(state, payload) {
      state.manholeTypeOpt = payload
    }
  },
  actions: {
    // 字典-井盖类型
    async ManholeTypeGet<S, T>({ commit }: ActionContext<S, T>) {
      return await getTenantDictDataDictionaryByCode({ code: 'manhole_type' }).then(
        (res: AxiosResponse) => {
          commit('MANHOLE_TYPE', res?.data?.data || [])
          return res?.data
        }
      )
    },
    //  井盖警报-列表
    async ManholeAlarmListGet<S, T>(_: ActionContext<S, T>, params: T) {
      return await manholeAlarmListGet<T>(params).then((res: AxiosResponse) => res?.data)
    },
    //  井盖警报-忽略
    async ManholeAlarmIgnoreAlarmGet<S, T>(_: ActionContext<S, T>, params: T) {
      return await manholeAlarmIgnoreAlarmGet<T>(params).then((res: AxiosResponse) => res?.data)
    },
    //  井盖警报-推送
    async ManholeAlarmPushOrderGet<S, T>(_: ActionContext<S, T>, params: T) {
      return await manholeAlarmPushOrderGet<T>(params).then((res: AxiosResponse) => res?.data)
    },
    //  井盖警报-配置
    async ManholeAlarmConfigGet<S, T>(_: ActionContext<S, T>, params: T) {
      return await manholeAlarmConfigGet<T>(params).then((res: AxiosResponse) => res?.data)
    },
    //  井盖警报-配置-修改
    async ManholeAlarmConfigUpdateGet<S, T>(_: ActionContext<S, T>, params: T) {
      return await manholeAlarmConfigUpdateGet<T>(params).then((res: AxiosResponse) => res?.data)
    },
    //  井盖设备-列表
    async ManholeListGet<S, T>(_: ActionContext<S, T>, params: T) {
      return await manholeListGet<T>(params).then((res: AxiosResponse) => res?.data)
    },
    // 设备列表-导出
    async ManholeExport<S, T>(_: ActionContext<S, T>, params: T) {
      return await manholeExport<T>(params).then((res: AxiosResponse) => {
        const contentDisposition = res?.headers?.['content-disposition']
        if (contentDisposition && res.data) {
          DownloadAttachment(decodeURI(contentDisposition.substring(20)), res.data)
        }
        return res.data
      })
    },
    // 设备列表-导入
    async ManholeImportExcelGet<S, T>(_: ActionContext<S, T>, params: T) {
      return await manholeImportExcelGet<T>(params).then((res: AxiosResponse) => res?.data)
    },
    //  井盖设备-删除
    async ManholeRemovePost<S, T>(_: ActionContext<S, T>, params: T) {
      return await manholeRemovePost<T>(params).then((res: AxiosResponse) => res?.data)
    },
    //  井盖设备-详情
    async ManholeViewGet<S, T>(_: ActionContext<S, T>, params: T) {
      return await manholeViewGet<T>(params).then((res: AxiosResponse) => res?.data)
    },
    //  井盖设备-新增
    async ManholeUpdatePost<S, T>(_: ActionContext<S, T>, params: T) {
      return await manholeUpdatePost<T>(params).then((res: AxiosResponse) => res?.data)
    },
    //  井盖设备-编辑
    async ManholeSavePost<S, T>(_: ActionContext<S, T>, params: T) {
      return await manholeSavePost<T>(params).then((res: AxiosResponse) => res?.data)
    },
    //  井盖设备-编辑
    async ManholeSyncAssetManholeGet<S, T>(_: ActionContext<S, T>, params: T) {
      return await manholeSyncAssetManholeGet<T>(params).then((res: AxiosResponse) => res?.data)
    },
    //  井盖-树
    async ManholeMapGetManholeTreeListGet<S, T>(_: ActionContext<S, T>, params: T) {
      return await manholeMapGetManholeTreeListGet<T>(params).then(
        (res: AxiosResponse) => res?.data
      )
    },
    //  井盖-点位
    async ManholeMapGetManholePointAllGet<S, T>(_: ActionContext<S, T>, params: T) {
      return await manholeMapGetManholePointAllGet<T>(params).then(
        (res: AxiosResponse) => res?.data
      )
    },
    //  井盖-设备状态统计
    async ManholeMapGetDeviceStateNumGet<S, T>(_: ActionContext<S, T>, params: T) {
      return await manholeMapGetDeviceStateNumGet<T>(params).then((res: AxiosResponse) => res?.data)
    },
    //  井盖-井盖设备列表
    async ManholeMapGetDeviceListGet<S, T>(_: ActionContext<S, T>, params: T) {
      return await manholeMapGetDeviceListGet<T>(params).then((res: AxiosResponse) => res?.data)
    },
    //  井盖-井盖设备列表
    async ManholeMapGetDeviceAlarmGet<S, T>(_: ActionContext<S, T>, params: T) {
      return await manholeMapGetDeviceAlarmGet<T>(params).then((res: AxiosResponse) => res?.data)
    },
    //  井盖-设备信息
    async ManholeMapGetDeviceInfoGet<S, T>(_: ActionContext<S, T>, params: T) {
      return await manholeMapGetDeviceInfoGet<T>(params).then((res: AxiosResponse) => res?.data)
    },
    //  井盖-设备工况列表
    async ManholeWorkListGet<S, T>(_: ActionContext<S, T>, params: T) {
      return await manholeWorkListGet<T>(params).then((res: AxiosResponse) => res?.data)
    }
  }
}

export default manholes
